<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="工具名称">
          <el-input
            v-model="formInline.name"
            placeholder="请输入工具名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否默认" class="movement">
          <el-select v-model="formInline.moren" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="时间">
          <el-date-picker v-model="formInline.value" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增工具</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="text item">
      <el-table
        v-loading="loading"
        :data="tableData"
        max-height="600"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="index"
          align="center"
          type="index"
          :index="indexMethod"
          label="序号"
          width="90"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="工具名称"
          width="180"
        >
        </el-table-column>
        <!-- <el-table-column prop="img" align="center" label="项目图片">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="wname" align="center" label="类型">
        </el-table-column>
        <el-table-column prop="sort" align="center" label="排序">
        </el-table-column>
        <el-table-column prop="createtime" align="center" label="创建时间">
        </el-table-column>
        <el-table-column prop="status" align="center" label="是否默认">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.moren"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              :inactive-value="null"
              active-text="是"
              inactive-text="否"
              @change="ifLook(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="sanChu(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[50, 100, 200, 300]"
        :page-size="50"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      :title="titleText"
    >
      <el-form
        :model="formLabelAlign"
        label-width="85px"
        ref="form"
        :rules="rules"
      >
        <div class="oneLine">
          <el-form-item label="工具名称" prop="name">
            <el-input
              v-model="formLabelAlign.name"
              placeholder="请输入工具名称"
            ></el-input>
          </el-form-item>
        </div>
        <div class="oneLine">
          <el-form-item label="工具分类" prop="types">
            <el-select v-model="formLabelAlign.types" placeholder="请选择">
              <el-option
                v-for="item in options1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="oneLine">
          <el-form-item label="排序" prop="sort">
            <el-input
              v-model="formLabelAlign.sort"
              placeholder="请输入排序序号"
            ></el-input>
          </el-form-item>
        </div>
        <!-- <div class="oneLine">
          <el-form-item label="头像" prop="imageUrl">
            <div class="unlodBox">
              <el-upload
                class="upload-demo"
                action
                :http-request="uploadFiles"
                ref="upload"
                :limit="fileLimit"
                :on-remove="handleRemove"
                :file-list="fileList"
                :on-exceed="handleExceed"
                :before-upload="beforeUpload"
                :show-file-list="false"
                :headers="headers"
              >
                <el-image
                  v-if="imageUrl"
                  class="imageGood"
                  :src="imageUrl"
                  style="width: 360px; height: 180px"
                ></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过5M
                </div>
              </el-upload>
            </div>
          </el-form-item>
        </div> -->
      </el-form>
      <div class="btnBox">
        <el-button type="primary" dialogVisible class="confirm" @click="save"
          >确 认</el-button
        >
        <el-button
          type="primary"
          dialogVisible
          class="confirm"
          @click="submitData()"
          >返 回</el-button
        >
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  opinions,
  uploadFiles,
  toolSet,
  setSort,
  goodsSonSort,
} from "../../../../utils/api";
import {
  getWeiXiuGj,
  addWeiXiuGj,
  editWeiXiuGj,
  delWeiXiuGj,
  getGongZhongSelect,
} from "../../../../utils/main";
export default {
  data() {
    return {
      options: [
        {
          value: "1",
          label: "默认",
        },
        {
          value: "null",
          label: "不默认",
        },
      ],
      options1: [],
      formLabelAlign: {
        name: "",
        types: "",
        sort: "",
      },
      //   上传图片
      //   fileList: [],
      //   headers: { "Content-Type": "multipart/form-data" },
      loading: false,
      token: sessionStorage.getItem("token"),
      dialogVisible: false,
      formInline: {
        moren: "",
        name: "",
      },
      tableData: [],
      parameter: {
        common: "",
      },
      total: 0,
      limit: 50,
      page: 1,
      type: "2",
      currentPage: "1",
      value: "",
      name: "",
      moren: "",
      titleText: "",
      wname: "",
      // dropdownindex: 0,
      types: null,
      sort: "",
      rules: {
        name: [{ required: true, message: "请输入工具名称", trigger: "blur" }],
        types: [{ required: true, message: "请选择工具分类", trigger: "blur" }],
      },
      // parent_id: 0,
    };
  },
  methods: {
    clearData() {
      this.formLabelAlign = {
        names: "",
        sort: "",
        type: "",
      };
    },
    ifLook(row) {
      console.log(row);
      editWeiXiuGj(
        this.token,
        row.name,
        row.moren,
        row.type,
        row.types,
        row.sort,
        row.id
      ).then((res) => {
        if (res.data.status) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
    // 获取工具类型
    async getSelect() {
      // this.loading = true;
      let res = await getGongZhongSelect(this.token, this.type)
        .then((res) => {
          if (res.data.status) {
            this.options1 = res.data.data;
            // console.log(this.options1);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //查询
    onSubmit() {
      this.name = this.formInline.name;
      this.moren = this.formInline.moren;
      // this.opinionsS();
      this.page = 1;
      this.getList();
    },
    // 重置
    onReset() {
      this.name = "";
      this.moren = "";
      this.formInline.name = "";
      this.formInline.moren = "";
      this.getList();
    },
    // 返回
    submitData() {
      this.clearData();
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    // goodsSonSort() {
    //   this.loading = true;
    //   this.fenleiList = [];
    //   goodsSonSort(
    //     this.token,
    //     this.currentPage,
    //     this.limit,
    //     this.topInputList[this.topInputList.length - 1].id,
    //     2
    //   ).then((res) => {
    //     if (res.data.status) {
    //       this.tableData = res.data.data;
    //       this.total = res.data.count;
    //       //status 1显示  2不显示   type

    //       this.tableData.forEach((item) => {
    //         item.aimages = [];
    //         if (item.image) {
    //           item.aimages.push("https://bjsf.bjsky.top" + item.image);
    //         }
    //       });

    //       let timer = setTimeout(() => {
    //         this.loading = false;
    //         clearTimeout(timer);
    //       }, 400);
    //       goodsSort(this.token, this.currentPage, this.limit, 2).then((res) => {
    //         if (res.data.status) {
    //           let list = res.data.data;
    //           list.forEach((item) => {
    //             this.fenleiList.push({
    //               id: item.id,
    //               name: item.name,
    //             });
    //           });
    //           this.fenleiList.unshift({
    //             id: 0,
    //             name: "顶级分类",
    //           });
    //         }
    //       });
    //     } else {
    //       if (res.data.msg == "token失效,请重新进行登录") {
    //         this.$router.push("/login");
    //       }
    //       this.$message.error(res.data.msg);
    //       this.loading = false;
    //     }
    //   });
    // },
    // 新增
    handleAdd() {
      // this.formLabelAlign = {};
      // this.formLabelAlign.name = "";
      // this.formLabelAlign.sort = "";
      // this.formLabelAlign.wname = "";
      this.clearData();
      this.dialogVisible = true;
      this.titleText = "新增工具";
    },
    // 编辑
    handleEdit(row) {
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign = obj;
      this.formLabelAlign.types = Number(obj.types);
      // this.options1.forEach((item) => {
      //   if (item.id == this.formLabelAlign.types) {
      //     this.formLabelAlign.types = item.name;
      //   }
      // });
      console.log(this.formLabelAlign, "obj");
      if (obj.moren == 1) {
        this.dialogVisible = false;
        this.$message({
          type: "warning",
          message: "显示状态下不能编辑",
        });
        return;
      }
      // console.log(this.formLabelAlign, 11111);
      this.dialogVisible = true;
      this.titleText = "编辑工具";
    },
    //确定添加
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // console.log(this.formLabelAlign, "8888880");
          if (this.formLabelAlign.id != null) {
            // console.log(this.formLabelAlign, "000");
            // this.options1.forEach((item) => {
            //   if (item.name == this.formLabelAlign.types) {
            //     this.formLabelAlign.types = item.id;
            //   }
            // });
            // 编辑
            editWeiXiuGj(
              this.token,
              this.formLabelAlign.name,
              this.formLabelAlign.moren,
              this.type,
              this.formLabelAlign.types,
              this.formLabelAlign.sort,
              this.formLabelAlign.id
            ).then((res) => {
              if (res.data.status) {
                console.log(this.formLabelAlign, 666666);
                this.$message({
                  message: "修改成功",
                });
                this.dialogVisible = false;
                this.clearData();
                this.$refs.form.resetFields();
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // console.log(this.formLabelAlign, "111");
            this.formLabelAlign.moren = "1";
            // 新增
            console.log(this.formLabelAlign);
            addWeiXiuGj(
              this.token,
              this.formLabelAlign.name,
              this.formLabelAlign.moren,
              this.type,
              this.formLabelAlign.types,
              this.formLabelAlign.sort
            ).then((res) => {
              if (res.data.status == true) {
                // console.log();
                this.$message({
                  message: "新增成功",
                });
                this.total = res.data.data.count;
                // this.formLabelAlign = {};
                this.dialogVisible = false;
                this.clearData();
                this.$refs.form.resetFields();
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 删除
    sanChu(row) {
      if (row.moren == 1) {
        this.dialogVisible = false;
        this.$message({
          type: "warning",
          message: "显示状态下不能删除",
        });
        return;
      }
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(row.id);
          delWeiXiuGj(this.token, row.id).then((res) => {
            if (res.data.status) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //详情
    // handleEdit(row) {
    //   this.parameter.common = row.common;
    //   this.dialogVisible = true;
    // },
    //序号
    indexMethod(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    //关闭
    handleClose() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    // handleClose(done) {
    //   done();
    // },
    //条数
    handleSizeChange(val) {
      this.limit = val;
      // this.opinionsS();
      this.getList();
    },
    //页数
    handleCurrentChange(val) {
      this.page = val;
      // this.opinionsS();
      this.getList();
    },
    // 获取列表数据
    async getList() {
      this.loading = true;
      let res = await getWeiXiuGj(
        this.token,
        this.page,
        this.limit,
        this.name,
        this.moren,
        this.type
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        // console.log(this.total);
        // console.log(this.tableData, "55555");
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    // 新增
    // handleRemove() {},
    //超出文件个数的回调
    // handleExceed() {
    //   this.$message({
    //     type: "warning",
    //     message: "超出最大上传文件数量的限制！",
    //   });
    //   return;
    // },
    //上传文件的格式
    // beforeUpload(file) {
    //   if (file.type != "" || file.type != null || file.type != undefined) {
    //     //截取文件的后缀，判断文件类型
    //     const FileExt = file.name.replace(/.+\./, "").toLowerCase();
    //     //计算文件的大小
    //     const isLt5M = file.size / 1024 / 1024 < 1; //这里做文件大小限制
    //     //如果大于50M
    //     if (!isLt5M) {
    //       this.$message.error("上传文件大小不能超过 1MB!");
    //       return false;
    //     }
    //     //如果文件类型不在允许上传的范围内
    //     if (this.fileType.includes(FileExt)) {
    //       return true;
    //     } else {
    //       this.$message.error("上传文件格式不正确!");
    //       return false;
    //     }
    //   }
    // },
    //上传文件的事件
    // uploadFiles(item) {
    //   this.$message("文件上传中........");
    //   //上传文件的需要formdata类型;所以要转
    //   uploadFiles(item.file).then((res) => {
    //     if (res.data.status != false || res.data.id != null) {
    //       console.log(res);
    //       res.data.data.saveurl; //半截
    //       res.data.data.weburl; //全拼
    //       this.imageUrl1 = res.data.data.saveurl;
    //       this.imageUrl = res.data.data.weburl;
    //       this.fileList.push(item.file); //成功过后手动将文件添加到展示列表里
    //       let i = this.fileList.indexOf(item.file);
    //       this.handleSuccess();
    //     }
    //   });
    // },
    //列表数据
    // opinionsS() {
    //   this.loading = true;
    //   opinions(
    //     this.token,
    //     this.page,
    //     this.limit,
    //     this.value,
    //     this.formInline.mobile,
    //     this.formInline.name
    //   )
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.tableData = res.data.data.data;
    //         this.total = res.data.data.count;
    //         this.tableData.forEach((item) => {
    //           if (item.avatar) {
    //             item.image = item.avatar;
    //             item.img = "https://bjsf.bjsky.top" + item.avatar;
    //           }
    //           if (item.ctime) {
    //             item.ctime_text = this.getNowTime(item.ctime * 1000);
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    // getNowTime(e) {
    //   let date = new Date(e);
    //   let year = date.getFullYear();
    //   let month = date.getMonth() + 1;
    //   let day = date.getDate();
    //   let hour = date.getHours();
    //   let minute = date.getMinutes();
    //   month = month < 10 ? "0" + month : month;
    //   day = day < 10 ? "0" + day : day;
    //   hour = hour < 10 ? "0" + hour : hour;
    //   minute = minute < 10 ? "0" + minute : minute;
    //   return year + "-" + month + "-" + day + " " + hour + ":" + minute;
    // },
  },
  mounted() {
    // this.opinionsS();
    this.getList();
    this.getSelect();
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.divFlex {
  display: flex;
  align-items: center;
}
.oneLine {
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}
.addP {
  font-size: 22px;
  text-align: center;
  margin-bottom: 10px;
}
/* 添加图片里的加号 */
/* .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
} */
.btnBox {
  display: flex;
  justify-content: center;
}
</style>